package com.yDev.stock_runner;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.graphics.Point;
import android.os.Build;
import android.os.Looper;
import android.util.Log;

public class DataThread extends Thread {

	private final static int timeoutConnection = 15000;
	private final static int timeoutSocket = 60000;

	String url;
	
	ArrayList<RigidBody> terrian= new ArrayList<RigidBody>();

	// desired fps
	private final static int MAX_FPS = 50;
	// maximum number of frames to be skipped
	private final static int MAX_FRAME_SKIPS = 5;
	// the frame period
	private final static int FRAME_PERIOD = 1000 / MAX_FPS;
	int mapToCenter;

	public DataThread(Context context, String api, ArrayList<RigidBody> terrian, int mapToCenter) {
		super();
		this.terrian=terrian;
		url = api;
		this.mapToCenter=mapToCenter;
	}

	public void run() {
		//Looper.prepare();
		long beginTime; // the time when the cycle begun
		long timeDiff; // the time it took for the cycle to execute
		int sleepTime; // ms to sleep (<0 if we're behind)
		int framesSkipped = 0; // number of frames being skipped
		Random rand =new Random(1000);
		while (true) {

			try {
				beginTime = System.currentTimeMillis();

				timeDiff = System.currentTimeMillis() - beginTime;
				sleepTime = (int) (FRAME_PERIOD - timeDiff);

				
/*			       HttpClient httpclient = new DefaultHttpClient();
			        HttpResponse response;
			        String responseString = null;
			        try {
			            response = httpclient.execute(new HttpGet(url));
			            StatusLine statusLine = response.getStatusLine();
			            if(statusLine.getStatusCode() == HttpStatus.SC_OK){
			                ByteArrayOutputStream out = new ByteArrayOutputStream();
			                response.getEntity().writeTo(out);
			                out.close();
			                responseString = out.toString();
			            } else{
			                //Closes the connection.
			               // response.getEntity().getContent().close();
			                throw new IOException(statusLine.getReasonPhrase());
			            }
			        } catch (ClientProtocolException e) {
			            //TODO Handle problems..
			        } catch (IOException e) {
			            //TODO Handle problems..
			        }*/
			        //Log.d("", responseString);
			       /* try {*/
						//JSONObject obj=new JSONObject(responseString);
						//String l=obj.optString("l");
						//int i=(int) (Double.parseDouble(l)*100);
						
						int i = rand.nextInt(20)+500;
						int a =terrian.size();
					//	Point p =new Point(a, i);
						//terrian.add(p);
						
						//int a =terrian.size();
						
						RigidBody r=new RigidBody();
						ydPoint p =new ydPoint(a*30, i);
						r.position=p;
						r.position.y+=mapToCenter;
						r.size=new ydPoint(30, 5);
						r.calcCornerPoints();
						if(terrian.size()<30)
						terrian.add(r);
				/*	} catch (JSONException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}*/
				//int i = rand.nextInt(2);
		
				if (sleepTime > 0) {
					try {
						Thread.sleep(sleepTime);
					} catch (InterruptedException e) {
					}
				}

				while (sleepTime < 0 && framesSkipped < MAX_FRAME_SKIPS) {
					// we need to catch up
					sleepTime += FRAME_PERIOD; // add frame period to check if
												// in next frame
					framesSkipped++;
				}

			} finally {

			}
		}
		
	}

}
